package com.hotel.controllers;

import com.hotel.entitys.Permission;
import com.hotel.entitys.RestPOJO;
import com.hotel.entitys.TreeNode;
import com.hotel.services.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="权限管理接口")//tags:说明信息
public class PermissionController {
    @Autowired
    private PermissionService permissionService;


    @ApiOperation(
            value = "查询权限通过电话",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/menus/{telephone}")
    public RestPOJO selPerByUser(@PathVariable("telephone") String telephone){
        RestPOJO restPOJO = new RestPOJO();
        List<Permission> list = permissionService.selPerByUser(telephone,0);
        if(!list.isEmpty()){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(list);
        }else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询权限通过员工角色id",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/pers/{rid}")
    public RestPOJO selPerTree(@PathVariable("rid") Integer rid){
        RestPOJO restPOJO = new RestPOJO();
        List<TreeNode> perTree = permissionService.selAllPer(0);
        List<Integer> perIds = permissionService.selPerByRid(rid);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("perlist",perTree);
        map.put("perIds",perIds);
        if(!perTree.isEmpty()){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(200);
            restPOJO.setData(map);
        }else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(500);
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "修改权限通过权限信息id和角色id",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/pers/updper")
    public RestPOJO updatePer(@RequestBody Map<String,Object> map){
        RestPOJO restPOJO = new RestPOJO();
        String rid = (String) map.get("rid");
        List<Integer> perids = (List<Integer>)map.get("perIds");
        int a = permissionService.delPerByRid(Integer.parseInt(rid));
        int b = permissionService.addPerByRid(Integer.parseInt(rid),perids);
        if(b > 0){
            restPOJO.setMessage("修改成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("修改失败");
            restPOJO.setStatus(500);
        }
        return restPOJO;
    }
}
